/**
 * 
 */
package br.srv.full.virtual.engine.message;

import javax.ejb.MessageDriven;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSession;
import javax.naming.InitialContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import br.srv.full.virtual.engine.users.PersonsManagerException;
import br.srv.full.virtual.entities.resume.UserLogin;

/**
 * @author Delfino
 *
 */
@Stateless(name = "MessageManager" )
@TransactionManagement(TransactionManagementType.CONTAINER)
public class MessageManagerEJB3 implements MessageManager, MessageManagerLocal {

	/**
	 * 
	 */
	private static final String QUEUE_VIRTUAL_USER_REGISTRATION = "queue/VirtualUserRegistration";
	private final Log log = LogFactory.getLog(getClass());


	public void sendMessageNewUserRegistration(UserLogin p_userLogin) throws PersonsManagerException {

		// TODO
		try {
			InitialContext ctx = new InitialContext();
			Object tmp = ctx.lookup("ConnectionFactory");
			QueueConnectionFactory tcf = (QueueConnectionFactory) tmp;
			QueueConnection conn = tcf.createQueueConnection();
			Queue queue = (Queue) ctx.lookup(QUEUE_VIRTUAL_USER_REGISTRATION);
			QueueSession session = conn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
			conn.start();

			MessageProducer producer = session.createProducer(queue);

			
			ObjectMessage msg = session.createObjectMessage(new NewUserRegistration(p_userLogin));

			producer.send(msg);

			producer.close();

			conn.stop();
			session.close();
			conn.close();

		} catch (Exception e) {
			log.warn(e);
		}

	}
}
